package org.eiahe.hr.training.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.github.linpeilie.Converter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.eiahe.hr.training.domain.vo.HrTrainingMaterialVo;
import org.eiahe.hr.training.domain.HrTrainingMaterial;
import org.eiahe.hr.training.domain.bo.HrTrainingMaterialBo;
import org.eiahe.hr.training.mapper.HrTrainingMaterialMapper;
import org.eiahe.hr.training.service.IHrTrainingMaterialService;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.ehe.common.satoken.utils.LoginHelper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 培训课件Service业务层处理
 *
 * @author zhangnn
 */
@RequiredArgsConstructor
@Service
public class HrTrainingMaterialServiceImpl implements IHrTrainingMaterialService {

    private final HrTrainingMaterialMapper baseMapper;
    private final Converter converter;

    /**
     * 查询培训课件
     */
    @Override
    public HrTrainingMaterialVo queryById(Long materialId) {
        return baseMapper.selectVoById(materialId);
    }

    /**
     * 查询培训课件列表
     */
    @Override
    public TableDataInfo<HrTrainingMaterialVo> queryPageList(HrTrainingMaterialBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<HrTrainingMaterial> lqw = buildQueryWrapper(bo);
        Page<HrTrainingMaterialVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询培训课件列表
     */
    @Override
    public List<HrTrainingMaterialVo> queryList(HrTrainingMaterialBo bo) {
        LambdaQueryWrapper<HrTrainingMaterial> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<HrTrainingMaterial> buildQueryWrapper(HrTrainingMaterialBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<HrTrainingMaterial> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getMaterialName()), HrTrainingMaterial::getMaterialName, bo.getMaterialName());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialType()), HrTrainingMaterial::getMaterialType, bo.getMaterialType());
        lqw.eq(StringUtils.isNotBlank(bo.getMaterialCategory()), HrTrainingMaterial::getMaterialCategory, bo.getMaterialCategory());
        lqw.eq(StringUtils.isNotBlank(bo.getIsPublic()), HrTrainingMaterial::getIsPublic, bo.getIsPublic());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), HrTrainingMaterial::getStatus, bo.getStatus());
        lqw.orderByAsc(HrTrainingMaterial::getSortOrder);
        lqw.orderByDesc(HrTrainingMaterial::getCreateTime);
        return lqw;
    }

    /**
     * 新增培训课件
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(HrTrainingMaterialBo bo) {
        HrTrainingMaterial add = converter.convert(bo, HrTrainingMaterial.class);
        add.setUploadBy(LoginHelper.getUserId());
        add.setUploadTime(new Date());
        add.setViewCount(0);
        add.setDownloadCount(0);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setMaterialId(add.getMaterialId());
        }
        return flag;
    }

    /**
     * 修改培训课件
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(HrTrainingMaterialBo bo) {
        HrTrainingMaterial update = converter.convert(bo, HrTrainingMaterial.class);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量删除培训课件
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteByIds(Collection<Long> materialIds) {
        return baseMapper.deleteByIds(materialIds) > 0;
    }

    /**
     * 增加查看次数
     */
    @Override
    public void increaseViewCount(Long materialId) {
        HrTrainingMaterial material = baseMapper.selectById(materialId);
        if (ObjectUtil.isNotNull(material)) {
            material.setViewCount(material.getViewCount() + 1);
            baseMapper.updateById(material);
        }
    }

    /**
     * 增加下载次数
     */
    @Override
    public void increaseDownloadCount(Long materialId) {
        HrTrainingMaterial material = baseMapper.selectById(materialId);
        if (ObjectUtil.isNotNull(material)) {
            material.setDownloadCount(material.getDownloadCount() + 1);
            baseMapper.updateById(material);
        }
    }

    /**
     * 根据部门权限查询课件列表(员工端)
     */
    @Override
    public List<HrTrainingMaterialVo> queryByDeptPermission(Long deptId) {
        LambdaQueryWrapper<HrTrainingMaterial> lqw = Wrappers.lambdaQuery();
        lqw.eq(HrTrainingMaterial::getStatus, "1");
        lqw.and(wrapper -> wrapper
            .eq(HrTrainingMaterial::getIsPublic, "1") // 公开的课件
            .or()
            .apply(StrUtil.isNotBlank(String.valueOf(deptId)),
                "FIND_IN_SET({0}, dept_ids)", deptId) // 部门可见的课件
            .or()
            .isNull(HrTrainingMaterial::getDeptIds) // 全员可见
        );
        lqw.orderByAsc(HrTrainingMaterial::getSortOrder);
        lqw.orderByDesc(HrTrainingMaterial::getCreateTime);
        return baseMapper.selectVoList(lqw);
    }

}
